<html lang="en">
	<head>
		<title>Yuka | Orientation</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			The entity is rotated by a defined angular step per second in order to face a specific target.
		</p>
	</section>

	<script type="module">

	import * as YUKA from '../../../build/yuka.module.js';
	import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';

	let renderer, scene, camera;

	let entityManager, time, entity, target;

	init();
	animate();

	function init() {

		scene = new THREE.Scene();

		camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 1000 );
		camera.position.set( 0, 0, 10 );
		camera.lookAt( scene.position );

		//

		const entityGeometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
		entityGeometry.rotateX( Math.PI * 0.5 );
		const entityMaterial = new THREE.MeshNormalMaterial();

		const entityMesh = new THREE.Mesh( entityGeometry, entityMaterial );
		entityMesh.matrixAutoUpdate = false;
		scene.add( entityMesh );

		const targetGeometry = new THREE.SphereBufferGeometry( 0.05 );
		const targetMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );

		const targetMesh = new THREE.Mesh( targetGeometry, targetMaterial );
		targetMesh.matrixAutoUpdate = false;
		scene.add( targetMesh );

		//

		const sphereGeometry = new THREE.SphereBufferGeometry( 2, 32, 32 );
		const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc, wireframe: true, transparent: true, opacity: 0.2 } );
		const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
		scene.add( sphere );

		// renderer

		renderer = new THREE.WebGLRenderer( { antialias: true } );
		renderer.setPixelRatio( window.devicePixelRatio );
		renderer.setSize( window.innerWidth, window.innerHeight );
		document.body.appendChild( renderer.domElement );

		//

		window.addEventListener( 'resize', onWindowResize, false );

		// game entity setup

		entityManager = new YUKA.EntityManager();
		time = new YUKA.Time();

		target = new YUKA.GameEntity();
		target.setRenderComponent( targetMesh, sync );

		entity = new YUKA.GameEntity();
		entity.maxTurnRate = Math.PI * 0.5;
		entity.setRenderComponent( entityMesh, sync );

		entityManager.add( entity );
		entityManager.add( target );

		//

		generateTarget();

	}

	function onWindowResize() {

		camera.aspect = window.innerWidth / window.innerHeight;
		camera.updateProjectionMatrix();

		renderer.setSize( window.innerWidth, window.innerHeight );

	}

	function animate() {

		requestAnimationFrame( animate );

		const delta = time.update().getDelta();

		entity.rotateTo( target.position, delta );

		entityManager.update();

		renderer.render( scene, camera );

	}

	function sync( entity, renderComponent ) {

		renderComponent.matrix.copy( entity.worldMatrix );

	}

	function generateTarget() {

		// generate a random point on a sphere

		const radius = 2;
		const phi = Math.acos( ( 2 * Math.random() ) - 1 );
		const theta = Math.random() * Math.PI * 2;

		target.position.fromSpherical( radius, phi, theta );

		setTimeout( generateTarget, 2000 );

	}

	</script>

</body>
</html>
